package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tencreat.common.core.domain.entity.SysDictData;
import com.tencreat.common.enums.BalanceBusinessType;
import com.tencreat.common.enums.BalanceTypeEnum;
import com.tencreat.common.enums.OrderType;
import com.tencreat.common.enums.mall.AllEnum;
import com.tencreat.common.utils.DictUtils;
import com.tencreat.common.utils.RabbitSendUtils;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.account.RechargeLimitChange;
import com.tencreat.service.account.IMallUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.tencreat.mapper.mall.RechargeLimitChangeMapper;
import com.tencreat.service.mall.IRechargeLimitChangeService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Stream;

/**
 * 代采购额度记录Service业务层处理
 *
 * @author tencreat
 * @company 深圳腾创互联科技有限公司
 * @link https://tencreat.com
 * @since 2023-11-15
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class RechargeLimitChangeServiceImpl extends ServiceImpl<RechargeLimitChangeMapper, RechargeLimitChange> implements IRechargeLimitChangeService {

    private final RabbitSendUtils rabbitUtils;
    private final IMallUserService mallUserService;

    /**
     * 分页查询
     *
     * @param iPage               分页参数
     * @param rechargeLimitChange 查询条件
     * @return 分页结果
     */
    @Override
    public IPage<RechargeLimitChange> page(IPage<?> iPage, RechargeLimitChange rechargeLimitChange) {
        return baseMapper.page(iPage, rechargeLimitChange);
    }

    /**
     * 描述信息  注册增加代采购额度、后台新增用户增加代采购额度
     *
     * @Author yaoruiqi
     * @Date: 2023/11/16 11:25
     */
    @Override
    public void change(MallUser mallUser) {
        //获取字典类型
        List<SysDictData> rechargeLimit = DictUtils.getDictCache("recharge_limit");
        if (rechargeLimit != null) {
            rechargeLimit.forEach(x -> {

                String[] split = mallUser.getClassRelations().split(",");
//                //当只有组织结构只有两个时
//                if (split.length <= 2) {
//                    if ("directReferred".equals(x.getDictLabel())) {
//                        rabbitUtils.sendBalanceMessage(Long.valueOf(split[1]), new BigDecimal(x.getDictValue()), AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.RECHARGE_DIRECT, "直推用户增加代采购额度", "直推用户增加代采购额度", BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);
//                    }
//                    // 间推
//                    if ("indirectReferred".equals(x.getDictLabel())) {
//                        rabbitUtils.sendBalanceMessage(Long.valueOf(split[0]), new BigDecimal(x.getDictValue()), AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.RECHARGE_INDIRECT, "间推用户增加代采购额度", "间推用户增加代采购额度", BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);
//                    }
//                }

                for (int i = split.length - 1; i >=0; i--) {
                    if (i == split.length - 1) {
                        // 直推
                        if ("directReferred".equals(x.getDictLabel())) {
                            rabbitUtils.sendBalanceMessage(Long.valueOf(split[i]), new BigDecimal(x.getDictValue()), AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.RECHARGE_DIRECT, "直推用户增加代采购额度", "直推用户增加代采购额度", BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);
                        }
                    } else if (i == split.length - 2) {
                        // 间推
                        if ("indirectReferred".equals(x.getDictLabel())) {
                            rabbitUtils.sendBalanceMessage(Long.valueOf(split[i]), new BigDecimal(x.getDictValue()), AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.RECHARGE_INDIRECT, "间推用户增加代采购额度", "间推用户增加代采购额度", BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);
                        }
                    } else {
                        // 无限级
                        if ("infinite".equals(x.getDictLabel())) {
                            rabbitUtils.sendBalanceMessage(Long.valueOf(split[i]), new BigDecimal(x.getDictValue()), AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.RECHARGE_TEAM, "团队用户增加代采购额度", "团队用户增加代采购额度", BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);
                        }
                    }
                }
                //注册赠送额度
                if ("registerSuccess".equals(x.getDictLabel())) {
                    rabbitUtils.sendBalanceMessage(mallUser.getUuid(), new BigDecimal(x.getDictValue()), AllEnum.BalanceChangeTypeEnums.INCOME.getType(), BalanceBusinessType.RECHARGE_REGISTER, "注册成功增加代采购额度", "注册成功增加代采购额度", BalanceTypeEnum.RECHARGE_LIMIT_CHANGE);
                }
            });

        }

    }

    @Override
    public BigDecimal changeByUser(Long uuid) {
        return baseMapper.changeByUser(uuid);
    }

    @Override
    public BigDecimal rechargeLimitRecover(LocalDateTime first, LocalDateTime end, Long uuid) {
        return baseMapper.rechargeLimitRecover(first,end,uuid);
    }
}
